package com.driving.code.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.driving.code.entity.Discuss;
import com.driving.code.entity.ParseData;
import com.driving.code.entity.Question;
import com.driving.code.entity.TUser;
import com.driving.code.service.DiscussService;
import com.driving.code.service.QuestionService;
import com.driving.code.service.TUserService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wen
 * @since 2022-02-01
 */
@RestController
@RequestMapping("/discuss")
public class DiscussController {

    @Resource
    private DiscussService discussService;
    @Resource
    private TUserService userService;
    @RequestMapping("/comment")
    public String comment(String comment, HttpSession session) {
        Integer questionId = (Integer) session.getAttribute("questionId");
        TUser user = (TUser) session.getAttribute("user");
        if (user==null){
            return "2";
        }
        Discuss discuss = new Discuss();
        discuss.setQuestionId(questionId);
        discuss.setCommentatorId(user.getUserId());
        discuss.setName(user.getUserName());
        discuss.setComment(comment);
        boolean save = discussService.save(discuss);
        if (save)
        return "1";
       else
           return "0";
    }
    @RequestMapping("/seeComment")
    public ParseData seeComment(@RequestParam(value = "page", required = false, defaultValue = "1") Integer current,
                                    @RequestParam(value = "limit", required = false, defaultValue = "5") Integer size,HttpSession session)
    {
        IPage<Discuss> page = new Page(current, size);
        LambdaQueryWrapper<Discuss> queryWrapper = new QueryWrapper<Discuss>().lambda();
        Integer questionId = (Integer) session.getAttribute("questionId");
        queryWrapper.eq(Discuss::getQuestionId,questionId);
        IPage<Discuss> discussIPage = discussService.page(page, queryWrapper);
        ParseData<Discuss> parseData = new ParseData<>();
        parseData.setData(discussIPage.getRecords());
        parseData.setCode(0);
        parseData.setCount(discussIPage.getTotal());
        parseData.setMsg("请求成功");
        return parseData;
    }

    @RequestMapping("/deleteDiscuss")
    public String deleteDiscuss(Integer id,String name,HttpSession session) {
        TUser user = (TUser) session.getAttribute("user");
        if (user==null){
            return "2";//未登录！
        }
        boolean b=false;
        if (user.getUserName().equals(name)||user.getUserId()==1){//系统最高权限管理人 以及本人才能删除评论
             b= discussService.removeById(id);
        }
        if (b)
            return "1";
        else
            return "0";//只能删除本人评论的内容
    }
}


